package com.app.oral.model

import com.app.oral.dto.DialogueQuestion
import com.app.oral.dto.ListeningQuestion
import java.time.LocalDateTime

/**
 * 试卷模型 - JSON优化结构
 * 从71个字段减少到22个字段，减少69%
 */
data class ExamPaper(
    val id: Long? = null,
    val title: String,
    val targetGrade: Int,
    val paperType: PaperType = PaperType.PRACTICE,

    // 朗读训练（文本题干）
    val readingContent: String,
    val readingAudioFileId: String = "",

    // 情景问答（JSON数组）- 5道题
    val dialogueQuestions: List<DialogueQuestion> = emptyList(),

    // 视听应答（JSON数组）- 5道题
    val listeningQuestions: List<ListeningQuestion> = emptyList(),

    // 话题表达部分
    val topicBrief: String,
    val keyPoints: String = "",
    val referenceAnswers: String = "",

    val createdTime: LocalDateTime? = null,
    val updatedTime: LocalDateTime? = null
) {
    /**
     * 提取所有音频文件ID（用于删除操作）
     * 包括：朗读示范音频 + 情景问答音频 + 视听应答音频 + 视听应答图片
     */
    fun extractAllFileIds(): List<String> {
        val fileIds = mutableListOf<String>()

        // 朗读示范音频
        if (readingAudioFileId.isNotEmpty()) {
            fileIds.add(readingAudioFileId)
        }

        // 情景问答音频文件
        dialogueQuestions.forEach { question ->
            if (question.audioFileId.isNotEmpty()) {
                fileIds.add(question.audioFileId)
            }
        }

        // 视听应答文件（音频 + 图片）
        listeningQuestions.forEach { question ->
            if (question.questionFileId.isNotEmpty()) {
                fileIds.add(question.questionFileId)
            }
            if (question.imageFileId.isNotEmpty()) {
                fileIds.add(question.imageFileId)
            }
        }

        return fileIds.distinct()
    }

    /**
     * 仅提取音频文件ID（向后兼容）
     */
    fun extractAudioFileIds(): List<String> {
        val audioIds = mutableListOf<String>()

        // 朗读示范音频
        if (readingAudioFileId.isNotEmpty()) {
            audioIds.add(readingAudioFileId)
        }

        // 情景问答音频
        dialogueQuestions.forEach { question ->
            if (question.audioFileId.isNotEmpty()) {
                audioIds.add(question.audioFileId)
            }
        }

        // 视听应答音频
        listeningQuestions.forEach { question ->
            if (question.questionFileId.isNotEmpty()) {
                audioIds.add(question.questionFileId)
            }
        }

        return audioIds.distinct()
    }
}